package com.epam.xml.export;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.jdom.Document;

import com.epam.xml.bank.Account;
import com.epam.xml.bank.Card;
import com.epam.xml.bank.Client;

public class JdomCreatorLauncher {
	private static Logger logger = Logger
			.getLogger(JdomCreatorLauncher.class);
	private static Random rand=new Random();
	
	public static void main(String[] args) {
		List<Client> clients = new ArrayList<Client>();
		for (int i=1; i<=4; i++) {
			Client client = new Client();
			client.setId(i);
			client.setLogin("login"+i);
			client.setPassword("password"+i);
			client.setName("name"+i);
			int numOfAccounts=0;
			if (i%2==0) {
				numOfAccounts=2;
			} else {
				numOfAccounts=1;
			}
			for (int j=0; j<numOfAccounts;j++) {
				HashMap<String, Integer> currencyAndAmount = new HashMap<String, Integer>();
				currencyAndAmount.put(randomCurrency(), rand.nextInt(500000));
				Card card=new Card(randomCardType());
				client.addAccount(new Account(i+j+1, currencyAndAmount, card));
			}
			clients.add(client);
		}
		JdomXMLCreator creator = new JdomXMLCreator();
		Document document = creator.createDocument(clients);
		if(creator.saveDocument("create_xml.xml", document)) {
			logger.info("The document has been created!");
		} else {
			logger.info("The document has not been created!");
		}
	}
	
	private static String randomCurrency() {
		switch(rand.nextInt(3)) {
		case 0:
			return "EUR";
		case 1: 
			return "USD";
		case 2:
			return "KZT";
		}
		return null;
	}
	
	private static String randomCardType() {
		switch(rand.nextInt(7)) {
		case 0:
			return "VISA";
		case 1:
			return "American Express";
		case 2:
			return "Master Card";
		case 3:
			return "Maestro";
		case 4:
			return "Cirrus";
		case 5:
			return "Diners Club";
		case 6:
			return "JCB";
		}
		return null;
	}
		
}
